<style scoped lang="less">
  .notice-card {
    width: 150px;
    /*cursor: pointer;*/
    margin: 10px;
    color: #333333;
    font-size: 15px;
    text-align: center;
  }

  .notice-num {
    /*color: #2d8cf0;*/
    font-size: 18px;
  }

  .notice-wrap {
    display: flex;
    margin-bottom: 10px;
  }

  .notice-card:first-child {
    margin-left: 0;
  }

  .notice-primary {
    background: #2d8cf0;
    color: white
  }

  .notice-info {
    background: #19be6b;
    color: white
  }

  .notice-warning {
    background-color: #ff9900;
    color: white;
  }

  .notice-import {
    background-color: #ed3f14;
    color: white;
  }
</style>
<template>
  <div>
    <div class="notice-wrap" v-if="noticeData!=null">

      <Card shadow class="notice-card notice-primary" to="/money/withdrawals-list">
        待处理提现
        <p class="notice-num">{{noticeData.withdrawals_count}}</p>
      </Card>
      <Card shadow class="notice-card notice-warning" to="/actives/active-list">
        即将结束活动
        <p class="notice-num">{{noticeData.active_about_end}}</p>
      </Card>
      <Card shadow class="notice-card" to="/feedback/suggest-list">
        待处理建议
        <p class="notice-num">{{noticeData.suggest_count}}</p>
      </Card>
      <Card shadow class="notice-card notice-import" to="/feedback/complain-list">
        待处理投诉
        <p class="notice-num">{{noticeData.complain_count}}</p>
      </Card>
      <Card shadow class="notice-card notice-info" to="/actives/comment-list">
        今日新增评价
        <p class="notice-num">{{noticeData.comment_count}}</p>
      </Card>
    </div>
    <Row :gutter="20">
      <i-col :xs="12" :md="8" :lg="4" v-for="(infor, i) in inforCardData" :key="`infor-${i}`"
             style="height: 120px;padding-bottom: 10px;">
        <infor-card :shadow="true" :color="infor.color" :icon="infor.icon" :icon-size="36">
          {{infor.count}}单/￥{{infor.amount/100}}
          <p>{{ infor.title }}</p>
        </infor-card>
      </i-col>
    </Row>
    <Card shadow>
      <div ref="dom" style="height: 310px;"></div>
    </Card>
  </div>
</template>
<script>
  import echarts from 'echarts'
  import {on, off} from '@/libs/tools'
  import {getActiveData, getNoticeData} from '@/api/statistics'
  import InforCard from '_c/info-card'

  export default {
    name: 'active-statistics',
    data() {
      return {
        dom: null,
        inforCardData: null,
        noticeData: null,
      };
    },
    components: {
      InforCard
    },
    created() {
      this.init();
    },
    beforeDestroy() {
      off(window, 'resize', this.resize)
    },
    methods: {
      setChartData(dates, amounts, counts) {
        const option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              label: {
                backgroundColor: '#6a7985'
              }
            }
          },
          grid: {
            top: '3%',
            left: '1.2%',
            right: '1%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              boundaryGap: false,
              data: dates
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '订单数',
              type: 'line',
              stack: '总量',
              areaStyle: {
                normal: {
                  color: '#2d8cf0'
                }
              },
              data: counts
            },
            {
              name: '金额',
              type: 'line',
              stack: '总量',
              areaStyle: {
                normal: {
                  color: '#10A6FF'
                }
              },
              data: amounts
            }
          ]
        };
        this.$nextTick(() => {
          this.dom = echarts.init(this.$refs.dom)
          this.dom.setOption(option)
          on(window, 'resize', this.resize)
        })
      },
      resize() {
        this.dom.resize()
      },
      init() {
        this.getOrderData();
        this.getNoticeData();
      },
      getNoticeData() {
        let vm = this;
        getNoticeData().then(rep => {
          vm.noticeData = rep.data.data;
        })
      },
      getOrderData() {
        let vm = this;
        getActiveData().then(rep => {
          vm.formatData(rep.data.data.counts);
          vm.initChart(rep.data.data.bydate);

        })
      },
      initChart(data) {
        let dates = [];
        let amounts = [];
        let counts = [];
        data.forEach(v => {
          dates.push(v.date);
          counts.push(v.count);
          amounts.push(v.total_amount / 100);
        });
        this.setChartData(dates, amounts, counts);
      },
      formatData(data) {
        let total = 0;//总计订单数
        let close = 0;//关闭订单数
        let closeAmount = 0;//关闭及退款金额
        let finished = 0;//完成订单数
        let finishedAmount = 0;//完成金额
        let unPay = 0;//未付款订单数
        let unPayAmount = 0;//未付款金额
        let payed = 0;//未核销订单数
        let payedAmount = 0;//未核销金额
        let exp = 0;//过期订单数
        let expAmount = 0;//过期金额
        let amount = 0;//总金额
        let vm = this;
        data.forEach(v => {
          total += v.count;
          amount += parseInt(v.total_amount);
          let result = vm.GLOBAL.getOrderStatus(v.pay_status, v.trade_status, null, 1);
          //订单类型 0 全部 1未付款 2待使用 3待收货 4待评价5已过期6已退款 10已关闭
          switch (result.status) {
            case 1:
              unPay += v.count;
              unPayAmount += parseInt(v.total_amount);
              break;
            case 2:
            case 3:
              payed += v.count;
              payedAmount += parseInt(v.total_amount);
              break;
            case 4:
              finished += v.count;
              finishedAmount += parseInt(v.total_amount);
              break;
            case 5:
              exp += v.count;
              expAmount += parseInt(v.total_amount);
              break;
            case 6:
            case 7:
            case 10:
              close += v.count;
              closeAmount += parseInt(v.total_amount);
              break;
          }
        });
        this.inforCardData = [
          {title: '总订单', icon: 'md-person-add', amount: amount, count: total, color: '#2d8cf0'},
          {title: '完成订单', icon: 'md-locate', amount: finishedAmount, count: finished, color: '#19be6b'},
          {title: '关闭(退款)订单', icon: 'md-help-circle', amount: closeAmount, count: close, color: '#ff9900'},
          {title: '未付款订单', icon: 'md-share', amount: unPayAmount, count: unPay, color: '#ed3f14'},
          {title: '过期订单', icon: 'md-chatbubbles', amount: expAmount, count: exp, color: '#E46CBB'},
        ];
      }
    }
  }
</script>